/**
  * 恢复合作
 */

<template lang='pug'>
  Modal(v-model="show" title="恢复合作" :closable="false" :mask-closable="false" width="700")
    Form(ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="125")
      FormItem.mb-10(label="档案号：")
        Input.input-has-border(v-model="formValidate.documentNo" type="text" style="width:100%" clearable)
      FormItem.mb-10(label="合同编号：")
        span {{contractCode}}
        //- Input(v-model="contractCode" type="text" style="width:100%" clearable)
      FormItem(label="合作年限：" prop="contractDuration")
        Input.input-has-border(v-model="formValidate.contractDuration" type="text" style="width:100%" clearable)
      FormItem(label="合同开始时间：" prop="contractStartDate")
        DatePicker.input-has-border(v-model="formValidate.contractStartDate" type="date" placement="bottom-start" placeholder="请选择" separator="至" style="width:100%" clearable)
      FormItem(label="合同结束时间：" prop="contractEndDate")
        DatePicker.input-has-border(v-model="formValidate.contractEndDate" type="date" placement="bottom-start" placeholder="请选择" separator="至" style="width:100%" clearable)
      FormItem(label="合作附件上传：" prop="fileString")
        Input(v-model="formValidate.fileString" style="display:none")
        Upload(:before-upload="handleUpload" multiple ref="upload" :show-upload-list="false" :action="baseUrl + 'api/Position/ImportPosition'")
          Button(icon="ios-cloud-upload-outline") 选择你需要上传的附件
      .d-f-c.mt-10(v-if="formValidate.file.length > 0" type="flex" justify="start")
        span.text-right 已选择文件：
        .content
          template(v-for="(item, index) in formValidate.file")
            .my-tag.mr-10.mb-5
              .my-tag-inner {{item.name}}
              .img-close.ivu-icon.ivu-icon-ios-close(@click="deleteFile(index)")
      FormItem(label="备注：")
        Input.input-has-border(v-model="formValidate.contractRemark" type="textarea" style="width:100%" clearable)
    template(slot="footer")
      Button(@click="cancel") 取消
      Button(type="info" :loading="loading" @click="handleSubmit('formValidate')") 确定
</template>

<script>
import dataApi from '@/api/data.js'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: "renew",
  props: {
    show: false,
    handleId: String,
    contractCode: String // 合同编号
  },
  data() {
    return {
      baseUrl: baseUrl,
      loading: false,
      formValidate: {
        documentNo: '', // 合同档案号
        contractStartDate: '', // 合同开始时间
        contractEndDate: '', // 合同结束时间
        contractDuration: '', // 合作年限
        file: [], // 合同附件
        fileString: '',
        contractRemark: '' // 合同备注
      },
      ruleValidate: {
        contractDuration: [
          { required: true, message: '合作年限不能为空', trigger: 'blur' }
        ],
        contractStartDate: [
          { required: true, type: 'date', message: '开始时间不能为空', trigger: 'change' }
        ],
        contractEndDate: [
          { required: true, type: 'date', message: '结束时间不能为空', trigger: 'change' }
        ],
        fileString: [
          { required: true,  message: '附件不能为空', trigger: 'change' }
        ]
      },
    }
  },

  computed: {},

  mounted() {
  },

  methods: {
    // 合同开始时间
    startDateChange (val) {
      this.formValidate.contractStartDate = val
    },
    // 结束时间
    endDateChange (val) {
      this.formValidate.contractEndDate = val
    },
    // 上传合同附件
    handleUpload (file) {
      this.formValidate.fileString = file.name
      this.formValidate.file.push(file)
      console.log(file)
      return false
    },
    deleteFile (index) {
      this.formValidate.file.splice(index, 1)
      if (this.formValidate.file.length < 1) {
        this.formValidate.fileString = ''
      }
    },
    // 恢复合作--取消
    cancel () {
      this.$emit('on-cancel')
    },
    // 恢复合作--确认
    resumeCooperateOk () {
      this.loading = true
      let formData = new FormData()
      this.formValidate.file.forEach(item => {
        formData.append('atta', item)
      })
      formData.append('code', this.contractCode)
      formData.append('documentNo', this.formValidate.documentNo)
      formData.append('startDate', this.$moment(this.formValidate.contractStartDate).format('YYYY-MM-DD'))
      formData.append('endDate', this.$moment(this.formValidate.contractEndDate).format('YYYY-MM-DD'))
      formData.append('duration', this.formValidate.contractDuration)
      formData.append('remark', this.formValidate.contractRemark)
      formData.append('customerId', this.handleId)
      dataApi.resumeCooperate(formData, this.handleId).then((res) => {
        this.loading = false
        if (res.data.success) {
          this.$Message.success(res.data.message)
          this.$emit('get-list')
          this.formValidate.documentNo = ''
          this.formValidate.file = []
          this.formValidate.contractRemark = ''
          this.$refs['formValidate'].resetFields();
        } else {
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.resumeCooperateOk()
        } else {
        }
      })
    }
  },

  watch: {

  }
};
</script>
<style lang='less' scoped>
.d-f-c{
  display: flex;
  align-items: flex-start;
  span{
    display: inline-block;
    width: 125px;
    padding-right: 12px;
  }
  .content{
    flex: 1;
  }
}
</style>